{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!pip install -q uagents google-genai"
      ],
      "metadata": {
        "id": "MFSuVchqTRGJ"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import os, time, multiprocessing, asyncio\n",
        "import nest_asyncio\n",
        "from google import genai\n",
        "from pydantic import BaseModel, Field\n",
        "from uagents import Agent, Context\n",
        "\n",
        "nest_asyncio.apply()"
      ],
      "metadata": {
        "id": "reTDtlVHWq85"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"GOOGLE_API_KEY\"] = \"Use Your Own API Key Here\"\n",
        "\n",
        "client = genai.Client()"
      ],
      "metadata": {
        "id": "F94gmWa8WtGH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class Question(BaseModel):\n",
        "    question: str = Field(...)\n",
        "\n",
        "class Answer(BaseModel):\n",
        "    answer: str = Field(...)"
      ],
      "metadata": {
        "id": "fEFjIgphWu5Y"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "ai_agent = Agent(\n",
        "    name=\"gemini_agent\",\n",
        "    seed=\"agent_seed_phrase\",\n",
        "    port=8000,\n",
        "    endpoint=[\"http://127.0.0.1:8000/submit\"]\n",
        ")\n",
        "\n",
        "@ai_agent.on_event(\"startup\")\n",
        "async def ai_startup(ctx: Context):\n",
        "    ctx.logger.info(f\"{ai_agent.name} listening on {ai_agent.address}\")\n",
        "\n",
        "def ask_gemini(q: str) -> str:\n",
        "    resp = client.models.generate_content(\n",
        "        model=\"gemini-2.0-flash\",\n",
        "        contents=f\"Answer the question: {q}\"\n",
        "    )\n",
        "    return resp.text\n",
        "\n",
        "@ai_agent.on_message(model=Question, replies=Answer)\n",
        "async def handle_question(ctx: Context, sender: str, msg: Question):\n",
        "    ans = ask_gemini(msg.question)\n",
        "    await ctx.send(sender, Answer(answer=ans))"
      ],
      "metadata": {
        "id": "3ZzBpbpiWwqL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "client_agent = Agent(\n",
        "    name=\"client_agent\",\n",
        "    seed=\"client_seed_phrase\",\n",
        "    port=8001,\n",
        "    endpoint=[\"http://127.0.0.1:8001/submit\"]\n",
        ")\n",
        "\n",
        "@client_agent.on_event(\"startup\")\n",
        "async def ask_on_start(ctx: Context):\n",
        "    await ctx.send(ai_agent.address, Question(question=\"What is the capital of France?\"))\n",
        "\n",
        "@client_agent.on_message(model=Answer)\n",
        "async def handle_answer(ctx: Context, sender: str, msg: Answer):\n",
        "    print(\"📨 Answer from Gemini:\", msg.answer)\n",
        "    # Use a more graceful shutdown\n",
        "    asyncio.create_task(shutdown_loop())\n",
        "\n",
        "async def shutdown_loop():\n",
        "    await asyncio.sleep(1)  # Give time for cleanup\n",
        "    loop = asyncio.get_event_loop()\n",
        "    loop.stop()"
      ],
      "metadata": {
        "id": "lQ4w-ZrQW2jZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def run_agent(agent):\n",
        "    agent.run()\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    p = multiprocessing.Process(target=run_agent, args=(ai_agent,))\n",
        "    p.start()\n",
        "    time.sleep(2)\n",
        "\n",
        "    client_agent.run()\n",
        "\n",
        "    p.join()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9IG9ao6VTpcs",
        "outputId": "53c9c1d3-7b9b-48ad-d8f9-b2a3b19392d4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "INFO:     [gemini_agent]: Starting agent with address: agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: Starting agent with address: agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: gemini_agent listening on agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: gemini_agent listening on agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qvsw5cmxhvm5de9a45ens4572srfjvtdsp67ewnjlrw2kre9wj0suq5k6ve\n",
            "INFO:     [gemini_agent]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)\n",
            "INFO:     [gemini_agent]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)\n",
            "INFO:     [uagents.registration]: Registration on Almanac API successful\n",
            "INFO:     [client_agent]: Starting agent with address: agent1qw08w2qxk3un2aued29z5s0hs9ava6mga8f9yay7a7lfnr0fqq0nv8he9ln\n",
            "INFO:     [client_agent]: Starting agent with address: agent1qw08w2qxk3un2aued29z5s0hs9ava6mga8f9yay7a7lfnr0fqq0nv8he9ln\n",
            "INFO:     [client_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8001&address=agent1qw08w2qxk3un2aued29z5s0hs9ava6mga8f9yay7a7lfnr0fqq0nv8he9ln\n",
            "INFO:     [client_agent]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8001&address=agent1qw08w2qxk3un2aued29z5s0hs9ava6mga8f9yay7a7lfnr0fqq0nv8he9ln\n",
            "INFO:     [client_agent]: Starting server on http://0.0.0.0:8001 (Press CTRL+C to quit)\n",
            "INFO:     [client_agent]: Starting server on http://0.0.0.0:8001 (Press CTRL+C to quit)\n",
            "WARNING:  [uagents.registration]: I do not have enough funds to register on Almanac contract\n",
            "INFO:     [uagents.registration]: Registration on Almanac API successful\n",
            "INFO:     [uagents.registration]: Registering on almanac contract...\n",
            "INFO:     [uagents.registration]: Adding testnet funds to fetch18tptxcz96cvezhyxjvjshgr9thasuagdy43vwc\n",
            "WARNING:  [uagents.registration]: Failed to register on Almanac contract due to insufficient funds\n",
            "INFO:     [uagents.registration]: Registering on almanac contract...complete\n",
            "INFO:     [uagents.registration]: Registration on Almanac API successful\n",
            "INFO:     [uagents.registration]: Registering on almanac contract...\n",
            "INFO:     [uagents.registration]: Registering on almanac contract...complete\n"
          ]
        }
      ]
    }
  ]
}